<?php

require MX_SHOP_PATH . 'core/web/mroles/page_merch.php';
class Index_MxShopPage extends MrolesWebPage {

    public function main(){

        global $_W;

        if(!$_W['isowner']){
            header('location: ' . mrolesUrl('index'));exit();
        }
        include $this->template('mroles/goods/index');
    }

    public function detail(){

        global $_W,$_GPC;

        if(!$_W['isowner']){
            header('location: ' . mrolesUrl('index'));exit();
        }

        $condition = ' AND og.uid=:uid ';
        $params = array(':uid'=>$_W['manageid']);

        if(!empty($_GPC['keyword'])){

            $type = $_GPC['type'];
            if($type == 'goods'){
                $condition .= ' AND locate(:keyword,g.name)>0 ';
            }else{
                $condition .= ' AND locate(:keyword,m.mendianname)>0 ';
            }
            $params[':keyword'] = htmlspecialchars_decode($_GPC['keyword'], ENT_QUOTES);
            $pageparams['type'] = $type;
            $pageparams['keyword'] = $_GPC['keyword'];
        }

        $selprov = trim($_GPC['province']);
        if (!empty($selprov) && $selprov != '请选择省份') {
            $condition .= ' AND m.province=:province ';
            $params[':province'] = $selprov;
            $pageparams['province'] = $selprov;

            $selcity = trim($_GPC['city']);
            if (!empty($selcity) && $selcity != '请选择城市') {
                $condition .= ' AND m.city=:city ';
                $params[':city'] = $selcity;
                $pageparams['city'] = $selcity;

                $seldist = trim($_GPC['dist']);
                if (!empty($seldist) && $seldist != '请选择区域' && $seldist != '市辖区') {
                    $condition .= ' AND m.dist=:dist ';
                    $params[':dist'] = $seldist;
                    $pageparams['dist'] = $seldist;
                }
            }
        }

        $pindex = empty($_GPC['page']) ? 1 : intval($_GPC['page']);
        $psize = 10;
        $list = pdo_fetchall('SELECT og.id,g.*,t.name as type,m.mendianname as mendian,m.province,m.city,m.dist FROM '.tablename('mx_shop_service_goods_owner').' og '.
                                    'LEFT JOIN '.tablename('mx_shop_service_goods').' g ON g.id=og.goods_id '.
                                    'LEFT JOIN '.tablename('mx_shop_service_goods_type').' t ON t.id=g.type_id '.
                                    'LEFT JOIN '.tablename('mx_shop_mendian').' m ON m.id=g.mendian_id '.
                                    'WHERE og.deleted=0 '.$condition.
                                    'LIMIT '.($pindex-1) * $psize.','.$psize, $params);

        $total = pdo_fetchcolumn('SELECT count(*) FROM '.tablename('mx_shop_service_goods_owner').' og LEFT JOIN '.tablename('mx_shop_service_goods').' g ON g.id=og.goods_id LEFT JOIN '.tablename('mx_shop_mendian').' m ON m.id=g.mendian_id WHERE og.deleted=0'.$condition, $params);
        $pager = owpager($total, $pindex, $psize, $pageparams);

        include $this->template('mroles/goods/detail');
    }

}